package org.springside.examples.miniservice.dao.user;

import java.util.List;

import org.springside.examples.miniservice.entity.user.User;
import org.springside.modules.orm.jpa.GenericDAO;

/**
 * User的DAO类.
 * 
 * 继承于泛型DAO基类GenericDAO,默认拥有增删改查的基本函数.
 * 用泛型定义管理的Entity类型为User,User的主键类型为Integer.
 * 
 * @see GenericDAO
 * @author calvin
 */
public class UserDAO extends GenericDAO<User, Integer> {

	//使用静态变量定义JPQL语句.
	private static final String COUNT_USER_BY_AUTH = "SELECT COUNT(user) FROM User user where user.loginName=? and password=?";

	public User findByLoginName(Object loginName) {
		return findUniqueByProperty("loginName", loginName);
	}

	public List<User> findByName(Object name, int... rowStartIdxAndCount) {
		return findByProperty("name", name, rowStartIdxAndCount);
	}

	public List<User> findByEmail(Object email, int... rowStartIdxAndCount) {
		return findByProperty("email", email, rowStartIdxAndCount);
	}

	/**
	 * 统计符合认证信息参数的帐号数量.
	 */
	public long countByAuthInfo(String loginName, String password) {
		return (Long) findUnique(COUNT_USER_BY_AUTH, loginName, password);
	}
}